// Implementation of lists, using doubly linked elements.
// (c) 1998, 2001 duane a. bailey
package Calculadora;
/**
 * An implementation of lists using doubly linked elements, similar to that of {@link java.util.LinkedList java.util.LinkedList}.
 * @version $Id: SinglyLinkedList.java,v 4.1 2000/12/29 02:39:16 bailey Exp bailey $
 * @author, 2001 duane a. bailey
 * @see SinglyLinkedList
 * @see CircularList
 * @since 12/08/2012
 * @version 1.0
 * @author Douglas Dávila
 */
public class DoublyLinkedList<E> extends AbstractList<E>
{
    /**
     * Number of elements within list.
     */
    protected int count;
    /**
     * Reference to head of list.
     */
    protected DoublyLinkedListElement head;
    /**
     * Reference to tail of list.
     */
    protected DoublyLinkedListElement tail;

    /**
     * Constructs an empty list.
     *
     * @post constructs an empty list
     * 
     */
    public DoublyLinkedList()
    {
	head = null;
	tail = null;
	count = 0;
    }
    /**
     * Add a value to head of list.
     *
     * @pre value is not null
     * @post adds element to head of list
     * 
     * @param value value to be added.
     */
    @Override
    public void addFirst(E value)
    {
	// construct a new element, making it head
	head = new DoublyLinkedListElement(value, head, null);
	// fix tail, if necessary
	if (tail == null) {
            tail = head;
        }
	count++;
    }

    /**
     * Remove a value from head of list.
     * Value is returned.
     *
     * @pre list is not empty
     * @post removes first value from list
     * 
     * @return value removed from list.
     */
    @Override
    public E removeFirst()
    {
	DoublyLinkedListElement temp = head;
	head = head.next();
	if (head != null) {
	    head.setPrevious(null);
	} else {
	    tail = null; // remove final value
	}
	temp.setNext(null);// helps clean things up; temp is free
	count--;
	return (E) temp.value();
    }

    /**
     * Get a copy of first value found in list.
     *
     * @pre list is not empty
     * @post returns first value in list
     * 
     * @return A reference to first value in list.
     */
  
    @Override
    public E getFirst()
    {
	return (E) head.value();
    }

    /**
     * Determine number of elements in list.
     *
     * @post returns number of elements in list
     * 
     * @return number of elements found in list.
     */
    @Override
    public int size()
    {
	return count;
    }
}


